package com.bac.rentmap.web.controller.ad.crawl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.bac.rentmap.ad.crawler.AdCrawler;
import com.bac.rentmap.ad.crawler.Context;

public class AdCrawlingController extends AbstractController {
    private AdCrawler<?> adCrawler;

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        Context context = new Context();
        String pageString = request.getParameter("page");
        if (pageString != null && !pageString.isEmpty()) {
            context.setPage(Integer.valueOf(pageString));
        }
        List<?> crawledAds = adCrawler.crawl(context);
        response
                .getWriter()
                .println(
                        "Following data was crawled from Web and stored to FlatCrawledAd table:");
        for (Object crawledAd : crawledAds) {
            response.getWriter().println(crawledAd);
        }
        return null;
    }

    public void setAdCrawler(AdCrawler<?> adCrawler) {
        this.adCrawler = adCrawler;
    }

}
